
</body>
</html>
<script src="http://cdn.static.runoob.com/libs/angular.js/1.4.6/angular.min.js"></script>
<script src="http://apps.bdimg.com/libs/angular-route/1.3.13/angular-route.min.js"></script>
<script src="http://apps.bdimg.com/libs/angular-resource/1.2.16/angular-resource.min.js"></script>
<script>
angular.module('restful',['ngRoute','ngResource'])
    .config(['$routeProvider',function($routeProvider){
        $routeProvider.
            when('/',{
                templateUrl:'/temp/index.html'
        }).
            when('/users',{
            templateUrl:'/temp/user.html',
            controller:'usersController'
        }).
            when('/articles',{
            templateUrl:'/temp/article.html',
            controller:'articlesController'
        }).
            otherwise({
                redirectTo:'/'
        });
    }])
    .factory('usersResource',['$resource',function($resource){
        return $resource('/users/:id',{id:'@id'},{
            update:{method:'PUT'}
        })
    }])
    .factory('articlesResource',['$resource',function($resource){
        return $resource('/articles/:id',{id:'@id'},{
            update:{method:'PUT'}
        })
    }])
    .controller('usersController',function($scope,usersResource){
        usersResource.query().$promise.then(function(data){
            $scope.users=data;
        })
        $scope.add=function(){
            if($scope.user.id){
                usersResource.update($scope.user).$promise.then(function(data){
                    $scope.user={};
                })
            }else{
                usersResource.save($scope.user).$promise.then(function(data){
                    $scope.users.push(data);
                    $scope.user={};
                })
            }
        }
        $scope.delete=function(user){
            usersResource.delete(user).$promise.then(function(data){
                $scope.users=$scope.users.filter(function(item){
                    if(item!=user){
                        return item;
                    }
                })
            })
        }
        $scope.edit=function(user){
            $scope.user=user;
        }
    })
    .controller('articlesController',function($scope,articlesResource){
        $scope.article={};
        articlesResource.query().$promise.then(function(data){
            $scope.articles=data;
        })
        $scope.add=function(){
            if($scope.article.id){
                articlesResource.update($scope.article).$promise.then(function(data){
                    $scope.article={};
                })
            }else{
                articlesResource.save($scope.article).$promise.then(function(data){
                    $scope.articles.push(data);
                    $scope.article={};
                })
            }
        }
        $scope.delete=function(article){
            articlesResource.delete(article).$promise.then(function(data){
                $scope.articles=$scope.articles.filter(function(item){
                    if(item!=article){
                        return item;
                    }
                })
            })
        }
        $scope.edit=function(article){
            $scope.article=article;
        }

    })
</script>
